package com.renwei.yunlong.activity.news;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.me.EnterpriseCertificationActivity;
import com.renwei.yunlong.activity.me.InvitationCompanyActivity;
import com.renwei.yunlong.activity.work.GuideWebActivity;
import com.renwei.yunlong.adapter.CompanyMsgPagerAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.FriendEnterpriseBean;
import com.renwei.yunlong.fragment.MessageCompanyFragment;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.DialogUtils;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MessageCompanyActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tablayout)
    TabLayout tablayout;
    @Bind(R.id.viewpager)
    ViewPager viewpager;
    CompanyMsgPagerAdapter adapter;
    private List<Fragment> mFragments;
    private boolean isAdmin = false;
    private FriendEnterpriseBean enterpriseBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_company);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initView();
    }

    private void initView() {
        simpleTileView.setTitle("邀请企业");
        TextView titleView = simpleTileView.getTitleView();
        Drawable titleLeft =getResources().getDrawable(R.mipmap.icon_title_help);
        titleLeft.setBounds(0, 0, titleLeft.getMinimumWidth(), titleLeft.getMinimumHeight());
        titleView.setCompoundDrawables(null, null, titleLeft, null);
        titleView.setCompoundDrawablePadding(10);
        titleView.setOnClickListener(this);

        if ("1".equals(companyType)) {
            if (AppHelper.isAdminOrServerDesk(ownerBean)) {
                isAdmin = true;
            } else {
                isAdmin = false;
            }
        } else {
            if (AppHelper.isAdminOrServerDesk(serviceLoginBean)) {
                isAdmin = true;
            } else {
                isAdmin = false;
            }
        }
        mFragments = new ArrayList<>();
        mFragments.add(new MessageCompanyFragment(true));
        mFragments.add(new MessageCompanyFragment(false));
        adapter = new CompanyMsgPagerAdapter(getSupportFragmentManager(), this, mFragments);
        viewpager.setAdapter(adapter);
        tablayout.setupWithViewPager(viewpager);
        tablayout.setTabMode(TabLayout.MODE_FIXED);
        simpleTileView.setRightOption(R.mipmap.icon_add, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAndAddCompany();
            }
        });
        refreshCompanyInfo();
    }

    public static void OpenActivity(Context context) {
        Intent intent = new Intent(context, MessageCompanyActivity.class);
        if (context instanceof Application) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }


    private void checkAndAddCompany() {
        if (isAdmin) {
            InvitationCompanyActivity.OpenActivity(MessageCompanyActivity.this);
        } else {
            showCenterInfoMsg("只有管理员有权限添加企业好友");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.option_textview_title:
                GuideWebActivity.openActivity(this, Api.$().GUIDE_INVITATION_COMP_URI);
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        super.onSuccess(requestId, result);
        switch (requestId) {
            case 879:
                enterpriseBean = new Gson().fromJson(result, FriendEnterpriseBean.class);
                if (enterpriseBean != null && enterpriseBean.getRows() != null) {
                    if (!"2".equals(StringUtils.value(enterpriseBean.getRows().getCheckFlag()))) {
                        String[] message = new String[2];
                        message[0] = "贵公司还未认证，无法邀请";
                        message[1] = "其他企业成为自己的好友企业";
                        showGuidView(message);
                    }
                }
                break;
            default:
                break;

        }
    }

    private void showGuidView(String[] message) {
        DialogUtils.showGuidePop(this, viewpager, R.mipmap.icon_guide_no_friend, message,
                new DialogUtils.OnGuideListener() {
                    @Override
                    public void onButtonClick(PopupWindow popupWindow, Button button) {
                        switch (StringUtils.value(button.getText())) {
                            case "开始引导":
                                GuideWebActivity.openActivity(mContext, Api.$().GUIDE_IDENT_COMP_URI);
                                break;
                            case "不用谢谢":
                                popupWindow.dismiss();
                                break;
                            case "马上认证":
                                Intent intent = new Intent(MessageCompanyActivity.this, EnterpriseCertificationActivity.class);
                                startActivity(intent);
                                break;
                            default:
                                break;

                        }
                    }
                }, "开始引导", "马上认证", "不用谢谢");
    }

}
